﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _1025HomeWork
{
    public class SquareClass
    {
        //用于生成随机数。
        //如果在每个对象的Move方法中新建随机数对象，
        //由于依次调用各对象的Move方法，间隔时间太短，
        //结果生成的随机数都是相同的。
        //所以在场地类中创建Random对象，
        //在各对象的Move方法中调用Random对象的Next方法
        public Random rnd = new Random();
        public int Width;
        public int height;
        //接口类型的列表
        public List<IMoveable> iF_List = new List<IMoveable>();
        //基类类型的列表
        public List<Animal> animalList = new List<Animal>();
        public List<Vehicle> vehicleList = new List<Vehicle>();
        public int[,] square;
        public char[,] squareName;
        public SquareClass(int _w = 5, int _h = 5) //默认]参数
        {
            Width = _w; height = _h;
            //创建存放场地的二维数组
            square = new int[Width, height];
            squareName = new char[Width, height];
            //创建场地上的动物和汽车
            IMoveable blackDog = new BlackDog(0, 1);
            IMoveable whiteDog = new WhiteDog(3, 2);
            IMoveable bigCattle = new BigCattle(4, 1);
            IMoveable smallCattle = new SmallCattle(1, 4);
            IMoveable truck = new Truck(2, 3);
            IMoveable car = new Car(4, 4);
            IMoveable suv = new SUVClass(2, 2);
            squareName[0, 1] = 'B';
            squareName[3, 2] = 'W';
            squareName[4, 1] = 'N';
            squareName[1, 4] = 'n';
            squareName[2, 3] = 'T';
            squareName[4, 4] = 'C';
            squareName[2, 2] = 'S';
            //将对象加入“接口引用”列表
            iF_List.Add(blackDog);
            iF_List.Add(whiteDog);
            iF_List.Add(bigCattle);
            iF_List.Add(smallCattle);
            iF_List.Add(truck);
            iF_List.Add(car);
            iF_List.Add(suv);
            //将对象加入“基类引用”列表，
            //注意没有新建对象，只是作强制转换后，向列表中添加引用。
            animalList.Add((Animal)blackDog);
            animalList.Add((Animal)whiteDog);
            animalList.Add((Animal)bigCattle);
            animalList.Add((Animal)smallCattle);
            //将对象加入依据基类引用创建列表
            vehicleList.Add((Vehicle)truck);
            vehicleList.Add((Vehicle)car);
            vehicleList.Add((Vehicle)suv);
        }
        public void Run()
        {
            //显示开始时的场地
            //如果栅格中有动物或汽车，该栅格保存对象的数量
            foreach (Animal an in animalList) square[an.X, an.Y]++;
            foreach (Vehicle vh in vehicleList) square[vh.X, vh.Y]++;
            //栅格内容输出到屏幕
            OutputToScreen();
            while (true)
            {
                //清除所有标记
                ClearFlags();
                //移动对象
                foreach (IMoveable im in iF_List) im.Move(this);
                //在场地上设置标志
                foreach (Animal an in animalList)
                {
                    square[an.X, an.Y]++;

                    //square[an.X, an.Y]++;
                    if (square[an.X, an.Y] == 1)
                    {
                        if (an.aName == "黑狗")
                        {
                            squareName[an.X, an.Y] = 'B';
                        }
                        else if (an.aName == "白狗")
                        {
                            squareName[an.X, an.Y] = 'W';
                        }
                        else if (an.aName == "大牛")
                        {
                            squareName[an.X, an.Y] = 'N';
                        }
                        else if (an.aName == "小牛")
                        {
                            squareName[an.X, an.Y] = 'n';
                        }
                    }

                }
                foreach (Vehicle vh in vehicleList)
                {
                    square[vh.X, vh.Y]++;
                    /*
                     if (square[vh.X, vh.Y] == 1)
                    {
                        if (vh.aName == "卡车")
                        {
                            squareName[vh.X, vh.Y] = 'T';
                        }
                        else if (vh.aName == "轿车")
                        {
                            squareName[vh.X, vh.Y] = 'C';
                        }
                        else if (vh.aName == "越野车")
                        {
                            squareName[vh.X, vh.Y] = 'S';
                        }
                    }
                     */
                }
                //输出到屏幕
                OutputToScreen();
                foreach (Animal an in animalList)
                {
                    //如果栅格上只有一个动物，动物发出叫声
                    if (square[an.X, an.Y] == 1)
                        Console.WriteLine(an.aName + ":" + an.Say());
                    //如果栅格上有多个动物，互相问好
                    if (square[an.X, an.Y] > 1) MakeSayHello(an);
                }
                foreach (Vehicle vh in vehicleList)
                {
                    if (square[vh.X, vh.Y] == 1)
                        Console.WriteLine(vh.aName + ":" + vh.Say());
                    if (square[vh.X, vh.Y] > 1) MakeSayHello(vh);
                }
                ConsoleKeyInfo info = Console.ReadKey(true);
                if (info.Key == ConsoleKey.Escape) break; //ESC键退出
            }
        }
        //检查参数传入的动物对象与两个列表中哪个对象的位置相同
        //若相同，向对方打招呼
        private void MakeSayHello(Animal _an)
        {
            for (int i = 0; i < animalList.Count; i++)
            {
                if (_an.X == animalList[i].X
                    && _an.Y == animalList[i].Y
                    && _an.aName != animalList[i].aName)
                    Console.WriteLine(_an.aName + ":" + _an.SayHello(animalList[i].aName));
            }
            for (int i = 0; i < vehicleList.Count; i++)
            {
                if (_an.X == vehicleList[i].X && _an.Y == vehicleList[i].Y)
                    Console.WriteLine(_an.aName + ":" + _an.SayHello(vehicleList[i].aName));
            }
        }
        //检查参数传入的汽车对象与两个列表中哪个对象的位置相同
        //若相同，向对方打招呼
        //注意：采用了方法重载
        private void MakeSayHello(Vehicle _vh)
        {
            for (int i = 0; i < vehicleList.Count; i++)
            {
                if (_vh.X == vehicleList[i].X
                    && _vh.Y == vehicleList[i].Y
                    && _vh.aName != vehicleList[i].aName)
                    Console.WriteLine(_vh.aName + ":"
 + _vh.SayHello(vehicleList[i].aName));
            }
            for (int i = 0; i < animalList.Count; i++)
            {
                if (_vh.X == animalList[i].X
                    && _vh.Y == animalList[i].Y
                    && _vh.aName != animalList[i].aName)
                    Console.WriteLine(_vh.aName + ":"
 + _vh.SayHello(animalList[i].aName));
            }
        }
        public void OutputToScreen() //场地现状输出到屏幕
        {
            string title = "  "; //场地的各列上方的编号
            for (int i = 0; i < height; i++) { title += i; }
            Console.WriteLine(title);
            string splitter = "  "; //分割线
            for (int i = 0; i < height; i++) { splitter += "-"; }
            Console.WriteLine(splitter);
            for (int i = 0; i < Width; i++)
            {
                Console.Write(i + "|");  //场地的各行左侧的编号和分割线
                for (int j = 0; j < height; j++)
                {
                    if (square[i, j] == 0) Console.Write(" ");


                    else if (square[i, j] == 1)
                    {
                        Console.Write(squareName[i, j]);
                    }
                    else
                    {
                        Console.Write(square[i, j]);
                    }


                }
                Console.WriteLine();
            }
            Console.WriteLine(splitter);
        }
        public void ClearFlags()//清空场地上所有标记
        {
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    square[i, j] = 0;
                }
            }
        }
    }
}
